package com.hrm.controller.insa;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.hrm.model.dto.insa.EducationDTO;
import com.hrm.model.dto.insa.EmployeeDTO;
import com.hrm.model.dto.insa.FamilyDTO;
import com.hrm.model.dto.insa.LanguageDTO;
import com.hrm.model.dto.insa.LicenceDTO;
import com.hrm.model.dto.insa.MilitaryDTO;
import com.hrm.model.dto.login.LoginDTO;
import com.hrm.model.service.FamilyService;
import com.hrm.model.service.EducationService;
import com.hrm.model.service.EmployeeService;
import com.hrm.model.service.LanguageService;
import com.hrm.model.service.LicenceService;
import com.hrm.model.service.MilitaryService;

public class ListAllController extends AbstractController{
	private EmployeeService employeeService;
	private LanguageService languageService;
	private FamilyService familyService;
	private LicenceService licenceService;
	private EducationService educationService;
	private MilitaryService militaryService;
	
	public void setLanguageService(LanguageService languageService) {
		this.languageService = languageService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public void setFamilyService(FamilyService familyService) {
		this.familyService = familyService;
	}

	public void setLicenceService(LicenceService licenceService) {
		this.licenceService = licenceService;
	}

	public void setEducationService(EducationService educationService) {
		this.educationService = educationService;
	}

	public void setMilitaryService(MilitaryService militaryService) {
		this.militaryService = militaryService;
	}

	private String getFindId(HttpServletRequest request) {
		
		String findid = (String)request.getSession().getAttribute("findid");
		if(findid == null){
			findid = ((LoginDTO)request.getSession().getAttribute("login")).getId();
			request.setAttribute("findid", findid);
		}
		return findid;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		EmployeeDTO employeeDTO = new EmployeeDTO();
		employeeDTO.setId(getFindId(request));
		employeeDTO = employeeService.getEmployee(employeeDTO);

		LanguageDTO languageDTO = new LanguageDTO();
		languageDTO.setId(employeeDTO.getId());
		List<LanguageDTO> langList = languageService.getAllLanguage(languageDTO);
		
		EducationDTO edto = new EducationDTO();
		edto.setId(employeeDTO.getId());
		List<EducationDTO> educationList = educationService.getEducation(edto);
		
		FamilyDTO dto = new FamilyDTO();
		dto.setId(employeeDTO.getId());
		List<FamilyDTO> familyList = familyService.getFamily(dto);
		
		LicenceDTO ldto = new LicenceDTO();
		ldto.setId(employeeDTO.getId());
		List<LicenceDTO> licenceList = licenceService.getAllLicence(ldto);
		
		MilitaryDTO militaryDTO = new MilitaryDTO();
		militaryDTO.setId(employeeDTO.getId());
		List<MilitaryDTO> militaryList = militaryService.getAllMilitary(militaryDTO);	

		ModelAndView mv = new ModelAndView();
		mv.addObject("employeeDTO", employeeDTO);
		mv.addObject("langList", langList);
		mv.addObject("educationlist", educationList);
		mv.addObject("FamilyList", familyList);
		mv.addObject("licencelist", licenceList);
		mv.addObject("militaryList", militaryList);
		mv.setViewName("listAll");

		return mv;
	}

}
